{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "import numpy as np\n",
    "from icmh5_reader import SignalProcessor\n",
    "from rich import print as rprint\n",
    "\n",
    "# for visualisation\n",
    "import plotly.graph_objects as go"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_segment(segment):\n",
    "    start_datetime = segment.start_datetime\n",
    "    sampling_freq = segment.sampling_freq\n",
    "    time_delta = datetime.timedelta(seconds=1/sampling_freq)\n",
    "    x_values = [start_datetime + i*time_delta for i in range(len(segment.values))]\n",
    "\n",
    "    fig = go.Figure()\n",
    "    fig.add_trace(go.Scatter(x=x_values, y=segment.values, mode='lines'))\n",
    "    fig.update_xaxes(type='date', tickformat='%H:%M:%S', tickmode='auto')\n",
    "    fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "to run the test: `pytest`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = 'data/aiden_hdf5_mock_pt2_wo_sep.icmh5' \n",
    "processor = SignalProcessor(file_path)\n",
    "signal_names = processor.get_available_signals()\n",
    "print(signal_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set one of the signals to be processed\n",
    "signal_name = signal_names[2]\n",
    "processor.set_signal(signal_name)\n",
    "\n",
    "# show the current info\n",
    "rprint(processor.current_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the whole data\n",
    "start_time, end_time = processor.current_info['start_time'], processor.current_info['end_time']\n",
    "data = processor.load_data(start_time, end_time-start_time)\n",
    "rprint(data.values.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_segment(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load all good segments\n",
    "segments = processor.load_segments(start_time=start_time, duration=end_time-start_time, quality_mode='good')\n",
    "rprint(len(segments))\n",
    "for segment in segments:\n",
    "    plot_segment(segment)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load all artefacts segments\n",
    "segments = processor.load_segments(start_time=start_time, duration=end_time-start_time, quality_mode='bad')\n",
    "rprint(len(segments))\n",
    "for segment in segments:\n",
    "    plot_segment(segment)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "processor.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "bpenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
